<?php

class Kd_Util_IO {

    public static function makeDirectory($path) {
        if (!is_dir($path)) {
            mkdir($path, 0700, TRUE);
        }
    }
    
    public static function moveFiles($file, $dir, $deleteSource = FALSE){
    	self::makeDirectory($dir);
    	$files = Kd_Util_Array::pack($file);
    	foreach($files as $file){
    		if($file == $dir.DS.basename($file)){
    			continue;
    		}
    		copy($file, $dir.DS.basename($file));
    		if($deleteSource){
    			@unlink($file);
    		}
    	}
    }
    
    public static function deleteFile($file){
    	$files = Kd_Util_Array::pack($file);
    	foreach($files as $file){
    		@unlink($file);
    	}
    }

    public static function getDirectories($path, $recursion = FALSE) {
        $result = array();
        if (is_dir($path)) {
            $handle = opendir($path);
            if ($handle) {
                while (false !== ($dir = readdir($handle))) {
                    if ($dir[0] != "." && is_dir($path . DIRECTORY_SEPARATOR . $dir)) {
                        $arr = array('name' => $dir, 'path' => str_replace('/', DIRECTORY_SEPARATOR, $path . DIRECTORY_SEPARATOR . $dir), 'subDirectory' => array());
                        if ($recursion) {
                            $arr['subDirectories'] = self::getDirectories($path . DIRECTORY_SEPARATOR . $dir, $recursion);
                        }
                        $result[] = $arr;
                    }
                }
                closedir($handle);
            }
        }
        return $result;
    }

    public static function getFiles($path, $type = '*') {
        $result = array();
        if (is_dir($path)) {
            $handle = opendir($path);
            if ($handle) {
                while (false !== ($dir = readdir($handle))) {
                    if ($dir[0] != "." && is_file($path . DIRECTORY_SEPARATOR . $dir)) {
                        $info = pathinfo($path . DIRECTORY_SEPARATOR . $dir);
                        $info['name'] = substr($dir, 0, strpos($dir, '.'));
                        $info['filename'] = $path . DIRECTORY_SEPARATOR . $dir;
                        if (is_array($type)) {
                            if (in_array($info['extension'], $type)) {
                                $result[] = $info;
                            }
                        } else {
                            if ($type == '*' || $info['extension'] == $type) {
                                $result[] = $info;
                            }
                        }
                    }
                }
                closedir($handle);
            }
            return $result;
        }
    }

    public static function findFile(array $directories, $subPath){
        if(substr($subPath, 0, 1) == DIRECTORY_SEPARATOR){
            $subPath = substr($subPath, 1);
        }
        foreach($directories as $directory){
            $dir = substr($directory, -1) == DIRECTORY_SEPARATOR? substr($directory, 0, -1): $directory;
            if(file_exists($dir.DIRECTORY_SEPARATOR.$subPath)){
                return $dir.DIRECTORY_SEPARATOR.$subPath;
            }
        }
        return NULL;
    }
    public static function readFile($path){
        $result ='';
        $file = fopen($path,'r') or die("Can't open file");
        $result = file_get_contents($path);
        fclose($file);
        return $result;
    }

    public static function writeFile($data, $path){
        if(!file_exists($path)){
            self::make(dirname($path));
        }
        
        $file = fopen($path, 'w+') or die("Can't open file");
        fwrite($file, $data);
        fclose($file);
    }
}